package com.ss.android.article.base.feature.search;

import android.location.Address;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.baselib.network.http.util.HttpQueryHelper;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.app.browser.GetEntryItemHelper;
import com.ss.android.article.browser.R;
import com.ss.android.common.location.LocationHelper;
import com.ss.android.newmedia.a.BrowserFragment;
import com.ss.android.newmedia.util.AppUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public final class r extends a {
    View p;
    View q;
    boolean r;
    String s;
    private FrameLayout t;
    private BrowserFragment u;
    private boolean v;

    public r() {

        this.v = true;
        this.r = false;
    }

    private static String a(double arg4) {
        String v0_1;
        try {
            v0_1 = String.format("%.5f", Double.valueOf(arg4));
        } catch (Exception v0) {
            v0_1 = null;
        }

        return v0_1;
    }

    public final String a() {
        char v1_3;
        StringBuilder v3_1;
        StringBuilder v2;
        String v0 = null;
        if (!android.support.design.a.isTextEmpty(this.l)) {
            try {
                v2 = new StringBuilder(String.format(ArticleConstants.apiWapSearchUrl, this.h, URLEncoder.encode(this.l, "UTF-8")));
                AppUtil.a(v2);
                if (this.m > 0) {
                    v2.append("&gid=").append(this.m);
                    v2.append("&item_id=").append(this.n);
                    v2.append("&aggr_type=").append(this.o);
                }

                v2.append("&search_sug=1");
                v2.append("&forum=1");
                Address v1_1 = LocationHelper.getInstance(this.getActivity()).loadSavedLocation();
                if (v1_1 != null && (v1_1.hasLatitude()) && (v1_1.hasLongitude())) {
                    String v3 = com.ss.android.article.base.feature.search.r.a(v1_1.getLatitude());
                    String v4 = com.ss.android.article.base.feature.search.r.a(v1_1.getLongitude());
                    if (!android.support.design.a.isTextEmpty(v3) && !android.support.design.a.isTextEmpty(v4)) {
                        v2.append("&latitude=").append(v1_1.getLatitude());
                        v2.append("&longitude=").append(v1_1.getLongitude());
                    }
                }

                String v1_2 = v2.indexOf("#") > 0 ? "&" : "#";
                v2.append(v1_2);
                v3_1 = v2.append("tt_daymode=");
                AppData.getInstance();
                if (AppData.isNightMode()) {
                    v1_3 = '0';
                } else {
                    v1_3 = '1';
                }

                label_74:
                v3_1.append(v1_3);
                v0 = v2.toString();
            } catch (UnsupportedEncodingException v1) {
                label_82:
                v1.printStackTrace();
            }
        }

        return v0;
    }

    public final void a(String arg3, boolean arg4) {
        super.a(arg3, arg4);
        this.q.setVisibility(View.GONE);
        if (android.support.design.a.isTextEmpty(arg3)) {
            arg3 = this.c.getText().toString().trim();
        }

        if (arg3.length() > 0) {
            this.l = arg3;
            if (this.v) {
                this.l();
            }
        }
    }

    public final int b() {
        return R.layout.i0;
    }

    public final int c() {
        return 0;
    }

    public final void e() {
        super.e();
        if (android.support.design.a.isTextEmpty(this.c.getText().toString())) {
            this.l = "";
            if ((this.v) && !AppData.getInstance().getAbSettings().isWebSearchEnable()) {
                this.t.setVisibility(View.GONE);
            }

            this.f.setVisibility(View.INVISIBLE);
        } else {
            this.f.setVisibility(View.VISIBLE);
        }

        if ((com.ss.android.article.base.feature.search.r.n()) && (this.i.equals(this.c.getHint().toString()))) {
            this.d.setEnabled(true);
            this.e.setEnabled(true);
        }
    }

    public final void f() {
        super.f();
        if (!android.support.design.a.isTextEmpty(this.g)) {
            if (!AppData.getInstance().getAbSettings().isWebSearchEnable()) {
                this.q.setVisibility(View.VISIBLE);
            }

            if (this.v) {
                this.l();
            }

            this.a(this.g);
        }
    }

    public final void h() {
        super.h();
        this.g = null;
        this.h = android.support.design.a.isTextEmpty(this.k) ? "search_tab" : this.k;
    }

    protected final void j() {
        if (!this.r && !AppData.getInstance().getAbSettings().isWebSearchEnable()) {
            this.q.setVisibility(View.VISIBLE);
        }
    }

    public final void k() {
        if (!this.r && !AppData.getInstance().getAbSettings().isWebSearchEnable()) {
            this.q.setVisibility(View.VISIBLE);
        }
    }

    public final void l() {
        super.l();
        if (this.t != null) {
            this.t.setVisibility(View.VISIBLE);
            String v0 = this.a();
            if (this.u == null) {
                this.u = this.m();
                Bundle v1 = new Bundle();
                v1.putString("bundle_url", v0);
                v1.putBoolean("bundle_use_day_night", true);
                this.u.setArguments(v1);
                this.getFragmentManager().beginTransaction().replace(com.ss.android.article.browser.R.id.a7o, this.u, "search_webview").commitAllowingStateLoss();
            }

            this.u.a(v0, true);
        }
    }

    public final BrowserFragment m() {
        GetEntryItemHelper v1 = new GetEntryItemHelper();
        v1.e = new GetEntryItemHelper.a$e(this);
        return ((BrowserFragment) v1);
    }

    final boolean o() {
        boolean v0 = true;
        try {
            if (!DBHelper.getInstance(this.getActivity()).getCityList(0).isEmpty()) {
                return false;
            }
        } catch (Exception v1) {
        }

        return v0;
    }

    public final void onActivityCreated(Bundle arg7) {
        super.onActivityCreated(arg7);
        if ((AppData.getInstance().getAbSettings().isWebSearchEnable()) && this.t != null) {
            UIUtils.setVisibility(this.t, 0);
            UIUtils.setVisibility(this.q, 8);
            HttpQueryHelper v1 = new HttpQueryHelper(ArticleConstants.searchSuggestWapInitialPage);
            String v2 = "homepage_search_suggest";
            String v0 = this.i == null || (com.ss.android.article.base.feature.search.r.n()) ? "" : this.i;
            v1.put(v2, v0);
            v1.put("from", "feed");
            if (!TextUtils.isEmpty(this.j)) {
                v1.put("sug_category", this.j);
            }

            StringBuilder v2_1 = new StringBuilder(v1.getEncodeQuery());
            AppUtil.a(v2_1);
            v0 = v2_1.indexOf("#") > 0 ? "&" : "#";
            v2_1.append(v0);
            StringBuilder v1_1 = v2_1.append("tt_daymode=");
            AppData.getInstance();
            char v0_1 = AppData.isNightMode() ? '0' : '1';
            v1_1.append(v0_1);
            v0 = v2_1.toString();
            if (this.u == null) {
                this.u = this.m();
                Bundle v1_2 = new Bundle();
                v1_2.putString("bundle_url", v0);
                v1_2.putBoolean("bundle_use_day_night", true);
                this.u.setArguments(v1_2);
                this.getFragmentManager().beginTransaction().replace(com.ss.android.article.browser.R.id.a7o, this.u, "search_webview").commitAllowingStateLoss();
            }

            this.u.a(v0, true);
        }

        Bundle v0_2 = this.getArguments();
        if (v0_2 != null) {
            this.r = v0_2.getBoolean("extra_hide_tips");
            this.s = v0_2.getString("from");
        }

        this.v = true;
        this.e();
        this.f();
        this.c.post(new s(this));
    }

    public final View onCreateView(LayoutInflater arg3, ViewGroup arg4, Bundle arg5) {
        View v1 = super.onCreateView(arg3, arg4, arg5);
        this.t = v1.findViewById(com.ss.android.article.browser.R.id.a7o);
        this.p = v1.findViewById(com.ss.android.article.browser.R.id.a7n);
        this.q = v1.findViewById(com.ss.android.article.browser.R.id.a94);
        return v1;
    }

    public final void onDestroy() {
        super.onDestroy();
    }

    public final void onResume() {
        super.onResume();
        this.c.postDelayed(new t(this), 400);
    }

    public final boolean p() {
        boolean v0 = false;
        if (this.c != null && this.b != null) {
            this.b.hideSoftInputFromWindow(this.c.getWindowToken(), 0);
        }

        if ((this.v) && this.u != null && (this.u.q())) {
            v0 = true;
        }

        return v0;
    }
}

